package com.first.nell.info

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.first.nell.R
import com.first.nell.components.form.*
import com.first.nell.data.RevObjInfoPageFieldConfig
import com.first.nell.enums.InfoPageActionEnum
import com.first.nell.utils.UsageCountryUtil

class InfoPageFieldsAdapter(private val dataList: List<RevObjInfoPageFieldConfig>) :
    RecyclerView.Adapter<InfoPageFieldsAdapter.ViewHolder>() {

    private var mFirstNellOnFirstNellItemFirstNellClickFirstNellListener: (Int, RevObjInfoPageFieldConfig) -> Unit =
        { positon, item -> }

    inner class ViewHolder(view: View, viewType: Int) : RecyclerView.ViewHolder(view) {
        var adpterFirstNellFormFirstNellInput: InputForm? = null
        var adpterFirstNellFormFirstNellSelect: SelectForm? = null
        var adpterFirstNellFormFirstNellSelectFirstNellGender: SelectGenderFrom? = null
        var adpterFirstNellFormFirstNellSelectFirstNellDate: SelectDateForm? = null
        var adpterFirstNellFormFirstNellSelectFirstNellCreditFirstNellReport: SelectCreditReportForm? =
            null

        var adpterFirstNellFormFirstNellSelectFirstNellAddess: SelectAddressForm? = null
        var adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellState: SelectAddressForm? =
            null
        var adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellCity: SelectAddressForm? =
            null
        var adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellArea: SelectAddressForm? =
            null

        var adpterFirstNellFormFirstNellSelfie: SelfieForm? = null
        var adpterFirstNellFormFirstNellSelectFirstNellBank: SelectBankForm? = null

        var textFirstNellAdpterFirstNellFormFirstNellSelectFirstNellContactFirstNellIndex: TextView? =
            null
        var adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellStatus: SelectForm? =
            null
        var adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellName: InputForm? =
            null
        var adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellPhone: InputForm? =
            null

        init {
            if (InfoPageActionEnum.INPUT.type == viewType) {
                adpterFirstNellFormFirstNellInput = view.findViewById(R.id.adpterFormInput)

            } else if (InfoPageActionEnum.SELECT.type == viewType) {
                adpterFirstNellFormFirstNellSelect = view.findViewById(R.id.adpterFormSelect)

            } else if (InfoPageActionEnum.BANK_PICK.type == viewType) {
                adpterFirstNellFormFirstNellSelectFirstNellBank =
                    view.findViewById(R.id.adpterFormSelectBank)

            } else if (InfoPageActionEnum.GENDER_PICK.type == viewType) {
                adpterFirstNellFormFirstNellSelectFirstNellGender =
                    view.findViewById(R.id.adpterFormSelectGender)

            } else if (InfoPageActionEnum.ADDRESS_PICK.type == viewType) {
                adpterFirstNellFormFirstNellSelectFirstNellAddess =
                    view.findViewById(R.id.adpterFormSelectAddress)

            } else if (InfoPageActionEnum.ADDRESS_STATE_PICK.type == viewType) {
                adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellState =
                    view.findViewById(R.id.adpterFormSelectAddressState)

            } else if (InfoPageActionEnum.ADDRESS_CITY_PICK.type == viewType) {
                adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellCity =
                    view.findViewById(R.id.adpterFormSelectAddressCity)

            } else if (InfoPageActionEnum.ADDRESS_AREA_PICK.type == viewType) {
                adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellArea =
                    view.findViewById(R.id.adpterFormSelectAddressArea)

            } else if (InfoPageActionEnum.DATE_PICK.type == viewType) {
                adpterFirstNellFormFirstNellSelectFirstNellDate =
                    view.findViewById(R.id.adpterFormSelectDate)

            } else if (InfoPageActionEnum.CONTACT_GROUP.type == viewType) {
                textFirstNellAdpterFirstNellFormFirstNellSelectFirstNellContactFirstNellIndex =
                    view.findViewById(R.id.textAdpterFormSelectContactIndex)
                adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellStatus =
                    view.findViewById(R.id.adpterFormSelectContactStatus)
                adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellName =
                    view.findViewById(R.id.adpterFormSelectContactName)
                adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellPhone =
                    view.findViewById(R.id.adpterFormSelectContactPhone)

            } else if (InfoPageActionEnum.CREDIT_REPORT.type == viewType) {
                adpterFirstNellFormFirstNellSelectFirstNellCreditFirstNellReport =
                    view.findViewById(R.id.adpterFormSelectCreditReport)

            } else if (InfoPageActionEnum.SELFIE.type == viewType) {
                adpterFirstNellFormFirstNellSelfie = view.findViewById(R.id.adpterFormSelfie)
            }
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        if (InfoPageActionEnum.INPUT.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_input, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.SELECT.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.BANK_PICK.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_bank, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.GENDER_PICK.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_gender, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.ADDRESS_PICK.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_address, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.ADDRESS_STATE_PICK.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_address_state, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.ADDRESS_CITY_PICK.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_address_city, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.ADDRESS_AREA_PICK.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_address_area, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.DATE_PICK.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_date, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.CONTACT_GROUP.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_contact, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.CREDIT_REPORT.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_select_credit_report, parent, false)
            return ViewHolder(view, viewType)

        } else if (InfoPageActionEnum.SELFIE.type == viewType) {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_selfie, parent, false)
            return ViewHolder(view, viewType)

        } else {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.view_first_nell_adpter_form_input, parent, false)
            return ViewHolder(view, viewType)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val viewType = getItemViewType(position)
        val data: RevObjInfoPageFieldConfig = dataList[position]

        if (InfoPageActionEnum.INPUT.type == viewType) {
            holder.adpterFirstNellFormFirstNellInput?.setFirstNellFormFirstNellConfig(data)

        } else if (InfoPageActionEnum.SELECT.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelect?.setFirstNellFormFirstNellConfig(data)

        } else if (InfoPageActionEnum.BANK_PICK.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelectFirstNellBank?.setFirstNellFormFirstNellConfig(
                data
            )
            holder.adpterFirstNellFormFirstNellSelectFirstNellBank?.setFirstNellOnFirstNellActionFirstNellClickFirstNellListener {
                mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
            }

        } else if (InfoPageActionEnum.GENDER_PICK.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelectFirstNellGender?.setFirstNellFormFirstNellConfig(
                data
            )

        } else if (InfoPageActionEnum.ADDRESS_PICK.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelectFirstNellAddess?.setFirstNellFormFirstNellConfig(
                data
            )
            holder.adpterFirstNellFormFirstNellSelectFirstNellAddess?.setFirstNellOnFirstNellActionFirstNellClickFirstNellListener {
                mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
            }

        } else if (InfoPageActionEnum.ADDRESS_STATE_PICK.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellState?.setFirstNellFormFirstNellConfig(
                data
            )
            holder.adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellState?.setFirstNellOnFirstNellActionFirstNellClickFirstNellListener {
                mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
            }

        } else if (InfoPageActionEnum.ADDRESS_CITY_PICK.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellCity?.setFirstNellFormFirstNellConfig(
                data
            )
            holder.adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellCity?.setFirstNellOnFirstNellActionFirstNellClickFirstNellListener {
                mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
            }

        } else if (InfoPageActionEnum.ADDRESS_AREA_PICK.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellArea?.setFirstNellFormFirstNellConfig(
                data
            )
            holder.adpterFirstNellFormFirstNellSelectFirstNellAddessFirstNellArea?.setFirstNellOnFirstNellActionFirstNellClickFirstNellListener {
                mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
            }

        } else if (InfoPageActionEnum.DATE_PICK.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelectFirstNellDate?.setFirstNellFormFirstNellConfig(
                data
            )
            holder.adpterFirstNellFormFirstNellSelectFirstNellDate?.setFirstNellOnFirstNellActionFirstNellClickFirstNellListener {
                mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
            }

        } else if (InfoPageActionEnum.CONTACT_GROUP.type == viewType) {
            if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() or UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
                holder.textFirstNellAdpterFirstNellFormFirstNellSelectFirstNellContactFirstNellIndex?.text =
                    "Contacter ${data.index}"
            } else if (UsageCountryUtil.isFirstNellCountryFirstNellVietnam()) {
                holder.textFirstNellAdpterFirstNellFormFirstNellSelectFirstNellContactFirstNellIndex?.text =
                    "Liên hệ ${data.index}"
            } else {
                holder.textFirstNellAdpterFirstNellFormFirstNellSelectFirstNellContactFirstNellIndex?.text =
                    "Contact ${data.index}"
            }

            data.subDataList?.let { list ->
                list.forEach { item ->
                    if (item.action.equals(
                            InfoPageActionEnum.CONTACT_GROUP_RELATION.name,
                            true
                        )
                    ) {
                        holder.adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellStatus?.setFirstNellFormFirstNellConfig(
                            item
                        )

                    } else if (item.isFirstNellActionFirstNellContactFirstNellGroupFirstNellName()) {
                        holder.adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellName?.setFirstNellFormFirstNellConfig(
                            item
                        )

                    } else if (item.isFirstNellActionFirstNellContactFirstNellGroupFirstNellPhone()) {
                        holder.adpterFirstNellFormFirstNellSelectFirstNellContactFirstNellPhone?.setFirstNellFormFirstNellConfig(
                            item
                        )
                    }
                }
            }
        } else if (InfoPageActionEnum.CREDIT_REPORT.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelectFirstNellCreditFirstNellReport?.setFirstNellFormFirstNellConfig(
                data
            )

        } else if (InfoPageActionEnum.SELFIE.type == viewType) {
            holder.adpterFirstNellFormFirstNellSelfie?.setFirstNellFormFirstNellConfig(data)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun getItemViewType(position: Int): Int {
        val data: RevObjInfoPageFieldConfig = dataList[position]
        val action = InfoPageActionEnum.valueOf(data.action.toString())
        return action.type
    }

    fun setFirstNellOnFirstNellItemFirstNellClickFirstNellListener(listener: (Int, RevObjInfoPageFieldConfig) -> Unit) {
        mFirstNellOnFirstNellItemFirstNellClickFirstNellListener = listener
    }
}